import axios from "axios"
import qs from 'qs'

const baseURL = '' // 'http://openapi.youdao.com/api'
const api = (url, data, config = {}) => {
  let _config = configDefaultGet()
  const configAssign = {
    ..._config,
    ...config,
  }
  const instance = axios.create(configAssign)
  const _data = qs.stringify(data, {
    arrayFormat: 'repeat'
  })
  return instance
    .request({
      url,
      data: _data,
    })
    .then((res) => {
      return res.data
    })
    .catch((err) => {
      console.log(err)
    })
}

const configDefaultGet = () => {
  const ContentType = {
    url: 'application/x-www-form-urlencoded;charset=utf-8',
    json: 'application/json;charset=utf-8'
  }
  return {
    baseURL: baseURL,
    headers: {
      "Content-Type": ContentType.url,
      // 'token': getToken() ? getToken() : ''
    },
    withCredentials: false,
    responseType: 'json',
    method: "post",
  }
}

export default api